//SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package board_dao

import (
	"context"

	"gitcode.com/eteam/api-server/dao/common"
	"gitcode.com/eteam/proto-gen-go.git/board_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type BasicBoardInfoDbItem struct {
	BoardName string `bson:"boardName"`
}

type NodePanelSettingDbItem struct {
	ShowSvgNode   bool `bson:"showSvgNode"`
	ShowImageNode bool `bson:"showImageNode"`
	ShowLinkNode  bool `bson:"showLinkNode"`
	ShowDevelop   bool `bson:"showDevelop"`
}

type BoardInfoDbItem struct {
	BoardId          string                 `bson:"_id"`
	BasicInfo        BasicBoardInfoDbItem   `bson:"basicInfo"`
	NodePanelSetting NodePanelSettingDbItem `bson:"nodePanelSetting"`
	ImageFsId        string                 `bson:"imageFsId"`
	CreateTime       int64                  `bson:"createTime"`
	UpdateTime       int64                  `bson:"updateTime"`
}

func (item *BoardInfoDbItem) ToBoardInfo() *board_api.BoardInfo {
	return &board_api.BoardInfo{
		BoardId: item.BoardId,
		BasicInfo: &board_api.BasicBoardInfo{
			BoardName: item.BasicInfo.BoardName,
		},
		NodePanelSetting: &board_api.NodePanelSetting{
			ShowSvgNode:   item.NodePanelSetting.ShowSvgNode,
			ShowImageNode: item.NodePanelSetting.ShowImageNode,
			ShowLinkNode:  item.NodePanelSetting.ShowLinkNode,
			ShowDevelop:   item.NodePanelSetting.ShowDevelop,
		},
		LlmList:    []*board_api.LlmInfo{},
		ImageFsId:  item.ImageFsId,
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}
}

type _BoardInfoDao struct {
	mc *mongo.Collection
}

func newBoardInfoDao(db *mongo.Database) (*_BoardInfoDao, error) {
	mc := db.Collection(common.DB_NS_BOARD + "info")
	return &_BoardInfoDao{mc: mc}, nil
}

func (db *_BoardInfoDao) Insert(ctx context.Context, dbItem *BoardInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_BoardInfoDao) Exist(ctx context.Context, boardId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": boardId})
	return count > 0, err
}

func (db *_BoardInfoDao) Get(ctx context.Context, boardId string) (*BoardInfoDbItem, error) {
	retItem := &BoardInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": boardId}).Decode(retItem)
	return retItem, err
}

func (db *_BoardInfoDao) Remove(ctx context.Context, boardId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": boardId})
	return err
}

func (db *_BoardInfoDao) ListById(ctx context.Context, boardIdList []string) ([]*BoardInfoDbItem, error) {
	if len(boardIdList) == 0 {
		return []*BoardInfoDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": boardIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*BoardInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_BoardInfoDao) ListAll(ctx context.Context) ([]*BoardInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{})
	if err != nil {
		return nil, err
	}
	retList := []*BoardInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_BoardInfoDao) UpdateBasic(ctx context.Context, boardId string, basicItem *BasicBoardInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": boardId}, bson.M{"$set": bson.M{
		"basicInfo":  basicItem,
		"updateTime": updateTime,
	}})
	return err
}

func (db *_BoardInfoDao) UpdateImageFsId(ctx context.Context, boardId, imageFsId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": boardId}, bson.M{"$set": bson.M{
		"imageFsId": imageFsId,
	}})
	return err
}

func (db *_BoardInfoDao) UpdateNodePanelSetting(ctx context.Context, boardId string, nodePanelSettingItem *NodePanelSettingDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": boardId}, bson.M{"$set": bson.M{
		"nodePanelSetting": nodePanelSettingItem,
		"updateTime":       updateTime,
	}})
	return err
}
